package com.ykdl.member.kid.set;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.ykdl.member.kid.R;
import com.ykdl.member.base.GlobalContext;
import com.ykdl.member.kid.gears.BaseScreen;

public class SetAboutUsActivity extends BaseScreen implements OnClickListener {

	private AlertDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_about_us);
		findViewsByID();
	}

	private void findViewsByID() {
		findViewById(R.id.two_email).setOnClickListener(this);// 给客服发邮件
		findViewById(R.id.two_phone).setOnClickListener(this);// 给客服打电话
		findViewById(R.id.two_weibo).setOnClickListener(this);// 微博
		findViewById(R.id.two_declare).setOnClickListener(this);// 免责
		findViewById(R.id.submit_idear).setOnClickListener(this);// 反馈意见
		findViewById(R.id.two_recommend).setOnClickListener(this);// 推荐好友
		findViewById(R.id.set_aboutus_div).setOnClickListener(this);// 免责
		findViewById(R.id.home).setOnClickListener(this);// 免责

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.home:
			this.finish();
			go(SettingActivity.class);
			break;
		case R.id.two_recommend:
			shareDialog(SetAboutUsActivity.this);
			break;
		case R.id.two_email:
			String[] receiver = new String[] { "lmyr@7500.com.cn" };
			String subject = "";
			String res = "";

			Intent email = new Intent(Intent.ACTION_SEND);
			email.setType("message/rfc822");
			// 设置邮件发收人
			email.putExtra(Intent.EXTRA_EMAIL, receiver);
			// 设置邮件标题
			email.putExtra(Intent.EXTRA_SUBJECT, subject);
			// 设置邮件内容
			email.putExtra(Intent.EXTRA_TEXT, res);
			// 调用系统的邮件系统
			startActivity(Intent.createChooser(email, "请选择邮件发送软件"));
			break;
		case R.id.two_phone:
			Intent intent_phone = new Intent(Intent.ACTION_CALL,
					Uri.parse("tel:010-57302471"));
			startActivity(intent_phone);
			break;
		case R.id.two_weibo:
			Intent intent_weibo = new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://m.weibo.com/lamayuerkehuduan"));
			startActivity(intent_weibo);
			break;
		case R.id.two_declare:
			// 免责声明
			Intent intentDec = new Intent(this, SetDeclare.class);
			intentDec.putExtra("id", R.id.two_declare);
			startActivity(intentDec);
			break;
		case R.id.set_aboutus_div:
			// 关于我们
			Intent intentAbout = new Intent(this, SetDeclare.class);
			intentAbout.putExtra("id", R.id.set_aboutus_div);
			startActivity(intentAbout);
			break;
		case R.id.submit_idear:
			// 意见反馈
			break;
		default:
			break;
		}

	}

	public void shareDialog(Context context) {
		LayoutInflater inflater = getLayoutInflater();
		View layout = inflater.inflate(R.layout.daily_tip_share_dialog,
				(ViewGroup) findViewById(R.id.share_dialog));
		dialog = new AlertDialog.Builder(this).setView(layout).show();

		Button smsForward = (Button) layout.findViewById(R.id.forward_sms);
		Button mailForward = (Button) layout.findViewById(R.id.forward_mail);
		Button mCancle = (Button) layout.findViewById(R.id.cancel);

		smsForward.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				doSMSForward();
				dialog.hide();
			}
		});
		mailForward.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				doMailForward2();
				dialog.hide();
			}
		});
		mCancle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
	}

	// 短信转发
	private void doSMSForward() {
		new AsyncTask<Void, Void, String>() {
			protected String doInBackground(Void... params) {
				String forwardmessage = null;
				try {
					forwardmessage = getText(R.string.recommend_content)
							+ GlobalContext.DOWNLOAD_URI;
				} catch (Exception e) {
					e.printStackTrace();
				}
				return forwardmessage;
			}

			@Override
			protected void onPostExecute(String result) {
				if (result != null) {
					Intent intent = new Intent(Intent.ACTION_SENDTO,
							Uri.parse("sms:"));
					intent.putExtra("sms_body", result);
					// Tools.startActivityAndReorder2Front(More.this, intent);
					go(intent);
				}
			}
		}.execute();
	}

	// 邮件转发2
	private void doMailForward2() {
		new AsyncTask<Void, Void, String>() {
			protected String doInBackground(Void... params) {
				String forwardmessage = null;
				try {
					forwardmessage = getText(R.string.recommend_content2)
							+ GlobalContext.DOWNLOAD_URI;
				} catch (Exception e) {
					e.printStackTrace();
				}
				return forwardmessage;
			}

			@Override
			protected void onPostExecute(String result) {
				if (result != null) {

					String[] receiver = new String[] { "" };
					String subject = "";

					Intent email = new Intent(Intent.ACTION_SEND);
					email.setType("message/rfc822");
					// 设置邮件发收人
					email.putExtra(Intent.EXTRA_EMAIL, receiver);
					// 设置邮件标题
					email.putExtra(Intent.EXTRA_SUBJECT, subject);
					// 设置邮件内容
					email.putExtra(Intent.EXTRA_TEXT, result);
					// 调用系统的邮件系统
					startActivity(Intent.createChooser(email, "请选择邮件发送软件"));
				}
			}
		}.execute();
	}

	@Override
	public void onBackPressed() {
		go(SettingActivity.class);
		finish();
	}

}
